package org.ovhoo.zonfig;

import java.util.ArrayList;


import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

/**
 * Implementation of a zonfig configuration file content handler
 * @author mzalim
 *
 */
public class ZonfigHandler implements ContentHandler{
	
	private int nbZonfig = 0;
	private int nbTypes = 0;
	
 	private ArrayList<Category> categories;
	private ArrayList<Type> types;
 	
	private Category tmpCategory ;	//Store the last found category
	private Frame tmpFrame ;		//Store the last found frame
	private Entry tmpEntry ;		//Store the last found entry
	private Type tmpType ;			//Store the last found type

	private String tmpValue;

	      	
	
	@Override
	public void characters(char[] ch, int start, int lenght) throws SAXException {
		//System.out.println("#PCDATA : " + new String(ch, start, lenght));
		if ( this.tmpValue != null){
			this.tmpValue += new String(ch, start, lenght);
		}
		else{
			//System.err.println("Chaine lu : "+ new String(ch, start, lenght));
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		
		// We compare only lower case words
		String _e_name = qName.toLowerCase();
		
		if (_e_name.equals("zonfig")){
			//this.eNzb = true;
			this.nbZonfig += 1;
		}
		else if (_e_name.equals("types")){
			//this.eNzb = true;
			this.nbTypes += 1;
		}
		else if(_e_name.equals("category")){
			this.tmpCategory = new Category(atts.getValue("title"));
		}
		else if(_e_name.equals("frame")){
			this.tmpFrame = new Frame(atts.getValue("title"));
		}
		else if(_e_name.equals("entry")){
			this.tmpEntry = new Entry(atts.getValue("name"), atts.getValue("type"));
		}
		else if(_e_name.equals("type")){
			this.tmpType = new Type(atts.getValue("name"), atts.getValue("base"));
			this.tmpType.setMax(atts.getValue("maxInt"), atts.getValue("maxDouble"));
			this.tmpType.setMin(atts.getValue("minInt"), atts.getValue("minDouble"));
		}
		else if (_e_name.equals("value")){
			this.tmpValue = new String("");
		}
		else if (_e_name.equals("forbidden")){
			this.tmpValue = new String("");
		}
		else {
			//null;
		}
	
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		String _e_name = qName.toLowerCase();
		
		if (_e_name.equals("zonfig")){
			
		}
		else if (_e_name.equals("types")){
			
		}
		else if (_e_name.equals("category")){
			this.categories.add(this.tmpCategory);
			this.tmpCategory = null;
		}
		else if (_e_name.equals("frame")){
			this.tmpCategory.addFrame(this.tmpFrame);
			this.tmpFrame = null;
		}
		else if (_e_name.equals("entry")){
			this.tmpFrame.addEntry(this.tmpEntry);
			this.tmpEntry = null;
		}
		else if (_e_name.equals("type")){
			this.types.add(this.tmpType);
			this.tmpType = null;
		}
		else if (_e_name.equals("value")){
			this.tmpType.addValue(this.tmpValue);
			this.tmpValue = null;
		}
		else if (_e_name.equals("forbidden")){
			this.tmpType.addForbidden(this.tmpValue);
			this.tmpValue = null;
		}
		else{
			//throw XMLSynatxException();
			System.err.println("ZonfgiHandler::endElement : impossible : " +_e_name);
		}
			
	}

	@Override
	public void endPrefixMapping(String arg0) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processingInstruction(String arg0, String arg1)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setDocumentLocator(Locator arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void skippedEntity(String arg0) throws SAXException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startDocument() throws SAXException {
		this.categories = new ArrayList<Category>();
		this.types = new ArrayList<Type>();
	}

	
	
	

	@Override
	public void startPrefixMapping(String arg0, String arg1)
			throws SAXException {
		// TODO Auto-generated method stub
		
	}
	
	
	
	/**
	 * Return the decodes categories
	 * @return the decoded categoies
	 */
	public ArrayList<Category> getCategories(){
		return this.categories;
	}
	
	/**
	 * Return the decodes types
	 * @return the decoded types
	 */
	public ArrayList<Type> getTypes(){
		return this.types;
	}
	
	
}


